#!/usr/bin/env python
# -*- coding:utf-8 -*-
from datetime import datetime

class FoodCatalogObj:
    READ_STATUS = {"READ": "1", "UNREAD": "0", "DROP": "9"}

    col_keys = ["id", "name", "classification", "unit_content_index", "heat", "measurement_unit", "calorie",
                "label_recognition", "explain","update_date"]
    col_def_val = ["", "", "", "", None]

    id: 0
    name: ""
    classification: ""
    unit_content_index: ""
    heat: ""
    measurement_unit: ""
    calorie: ""
    label_recognition: ""
    explain: ""
    update_date: datetime

    def __init__(self):
        return

    @staticmethod
    def make_catalog_data(data):
        for item_index in range(len(FoodCatalogObj.col_keys)):
            try:
                _check_val = data[str(FoodCatalogObj.col_keys[item_index]).lower()]
                if (isinstance(_check_val, int) or isinstance(_check_val, float)):
                    if _check_val is None:
                        _val = FoodCatalogObj.col_def_val[item_index]
                    else:
                        if isinstance(_check_val, int):
                            _val = int(_check_val)
                        elif isinstance(_check_val, float):
                            _val = float(_check_val)
                        else:
                            _val = round(_check_val, 2)
                else:
                    if _check_val is not None and len(_check_val) > 0:
                        _val = data[str(FoodCatalogObj.col_keys[item_index]).lower()]  # 这里获取页面数值
                    else:
                        _val = FoodCatalogObj.col_def_val[item_index]  # 这里配置数据库默认值
            except KeyError as e:
                _val = ''
            data[str(FoodCatalogObj.col_keys[item_index]).upper()] = _val

    @staticmethod
    def split_params(params):
        g_param = []
        if params['name'] is not None and len(params['name']) > 0:
            g_param.append('`name`=' + "\'" + params['name'] + "\'")
        if params['classification'] is not None and len(params['classification']) > 0:
            g_param.append('`classification`=' + "\'" + params['classification'] + "\'")
        if params['unit_content_index'] is not None and len(params['unit_content_index']) > 0:
            g_param.append('`unit_content_index`=' + "\'" + params['unit_content_index'] + "\'")
        if params['heat'] is not None and len(params['heat']) > 0:
            g_param.append('`heat`=' + "\'" + params['heat'] + "\'")
        if params['measurement_unit'] is not None and len(params['measurement_unit']) > 0:
            g_param.append('`measurement_unit`=' + "\'" + params['measurement_unit'] + "\'")
        if params['calorie'] is not None and len(params['calorie']) > 0:
            g_param.append('`calorie`=' + "\'" + params['calorie'] + "\'")
        if params['label_recognition'] is not None and len(params['label_recognition']) > 0:
            g_param.append('`label_recognition`=' + "\'" + params['label_recognition'] + "\'")
        if params['explain'] is not None and len(params['explain']) > 0:
            g_param.append('`explain`=' + "\'" + params['explain'] + "\'")
        return ', '.join(str(i) for i in g_param)

    @staticmethod
    def concat_datas(g_param):
        from datetime import datetime
        # 获取当前时间
        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        arry = [g_param["name"], g_param["classification"], g_param["unit_content_index"], g_param["heat"],
                g_param["measurement_unit"], g_param["calorie"], g_param["label_recognition"], g_param["explain"],
                current_time ]
        _res = []
        for _val in arry:
            if isinstance(_val, str):
                _res.append(f"\'{_val}\'")
            else:
                if _val is None:
                    _res.append("null")
                else:
                    _res.append(_val)
        return ', '.join(str(i) for i in _res)

    @staticmethod
    def load_db_2_dict(data):
        g_param = {}
        g_param["id"] = data[0]
        g_param["name"] = data[1]
        g_param["classification"] = data[2]
        g_param["unit_content_index"] = data[3]
        g_param["heat"] = str(data[4])
        g_param["measurement_unit"] = str(data[5])
        g_param['calorie'] = str(data[6])
        g_param['label_recognition'] = str(data[7])
        g_param['explain'] = str(data[8])
        g_param['update_date'] = str(data[8])
        return g_param

    @staticmethod
    def init_dict():
        g_param = {}
        g_param["id"] = 0
        g_param["name"] = ""
        g_param["classification"] = ""
        g_param["unit_content_index"] = 0
        g_param["heat"] = ""
        g_param["measurement_unit"] = ""
        g_param['calorie'] = ""
        g_param['label_recognition'] = ""
        g_param['explain'] = ""
        g_param['update_date'] = ""
        return g_param

    @staticmethod
    def to_string(with_flg):
        g_param = []
        if with_flg:
            g_param.append("`mem_food_catalogs`.`name`")
            g_param.append("`mem_food_catalogs`.`classification`")
            g_param.append("`mem_food_catalogs`.`unit_content_index`")
            g_param.append("`mem_food_catalogs`.`heat`")
            g_param.append("`mem_food_catalogs`.`measurement_unit`")
            g_param.append("`mem_food_catalogs`.`calorie`")
            g_param.append("`mem_food_catalogs`.`label_recognition`")
            g_param.append("`mem_food_catalogs`.`explain`")
            g_param.append("`mem_food_catalogs`.`update_date`")
        else:
            g_param.append("`name`")
            g_param.append("`classification`")
            g_param.append("`unit_content_index`")
            g_param.append("`heat`")
            g_param.append("`measurement_unit`")
            g_param.append("`calorie`")
            g_param.append("`label_recognition`")
            g_param.append("`explain`")
            g_param.append("`update_date`")
        return ', '.join(str(i) for i in g_param)
